﻿/// <reference path="http://localhost:3091/Scripts/jquery-1.4.1-vsdoc.js" />

//DateControl class
// constructor
function DateControl(ddlMonthsId,ddlYearsId,ddlDaysId)
{
    // member variables
    this._ddlMonths = ddlMonthsId;
    this._ddlYears = ddlYearsId;
    this._ddlDays = ddlDaysId;

    this.Initilize();
}

//methods
DateControl.prototype =
{
    Initilize: function ()
    {
        var _this = this;
        $("#" + this._ddlMonths).change(function () { _this.PopulateDays(); });
        $("#" + this._ddlYears).change(function () { _this.PopulateDays(); });

    },

    PopulateDays: function ()
    {
        var monthVal = $("#" + this._ddlMonths).val();
        var yearVal = $("#" + this._ddlYears).val();
        //--------------------------------------------
        if (monthVal != 0 && yearVal != 0)
        {
            var dayCount = 32 - new Date(yearVal, monthVal - 1, 32).getDate(); // get days number;
            var daysElement = $("#" + this._ddlDays); // get jquery object contains the days element
            var seletedDate = daysElement.val(); // get selected item
            daysElement.find('option').remove(); // clear all options
            for (var i = 1; i <= dayCount; i++)
            {
                daysElement.append("<option>" + i + "</option>").val(i);
            }

            daysElement.val(1);
            if (daysElement.find('option[value=' + seletedDate + ']').length > 0) //set the last selected item
                daysElement.val(seletedDate);
                
        }
    }
}